<?php

namespace App\Http\Controllers\Backend;
use App\Common\ResponseCode;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\AdminAuth;
use App\Services\Goods\AttributeService;
use App\Traits\VerifyRequestParam;
use Illuminate\Http\Request;
/**
 * 商品属性控制器
 *
 * @package App\Http\Controllers\H5
 * @author WangBin
 * @date 2021-06/11
 */
class AttributeController extends BaseController
{
    use VerifyRequestParam;

    public function __construct()
    {

    }

    /**
     * 获取属性详情
     *
     * @return
     * @throws
     */
    public function get()
    {
        $ret = AttributeService::getInstance()->get($this->verifyInteger('id'));
        return $this->success($ret);
    }

    /**
     * 保存属性
     *
     * @param Request $request
     * @throws \App\Exceptions\BusinessException
     * @return
     */
    public function update()
    {
        $data = [
            'attribute_id' => $this->verifyInteger('attribute_id', 0),
            'name' => $this->verifyString('name'),
            'attr_bn' => $this->verifyString('attr_bn'),
            'is_active' => $this->verifyInteger('is_active', 1),
            'intro' => $this->verifyString('intro', '')
        ];
        $ret = AttributeService::getInstance()->update($data);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::GOODS_ATTR_UPDATE_FAIL);
    }

    /**
     * 属性列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws
     */
    public function getList(Request $request)
    {
        $filter = [
            'name' => $this->verifyString('spec_name', ''),
            'attr_bn' => $this->verifyString('spec_bn', ''),
            'limit' => $this->verifyInteger('limit', 20),
            'page' => $this->verifyInteger('page', 1) - 1,
        ];
        $ret = AttributeService::getInstance()->getList($filter);
        return $this->success($ret);
    }

    /**
     * 删除属性
     *
     * @throws \App\Exceptions\BusinessException
     * @return
     */
    public function delete()
    {
        $id = $this->verifyString('id');
        $ret = AttributeService::getInstance()->delete($id);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::GOODS_ATTR_DELETE_FAIL);
    }

    /**
     * 停用/启用属性
     *
     * @throws \App\Exceptions\BusinessException
     * @return
     */
    public function enable()
    {
        $id = $this->verifyString('id');
        $ret = AttributeService::getInstance()->enable($id);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::GOODS_ATTR_ENABLE_FAIL);
    }
}
